﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Phone
{
    //自定义信号设置界面
    public partial class Signals : Form
    {
        //默认构造函数
        public Signals()
        {
            InitializeComponent();
        }

        //窗体加载时读取自定义信号列表并显示
        private void Signals_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Defines.MAX_CUSTOM_SIGNAL_COUNT; i++)
            {
                listBox1.Items.Add(Vars.custom_signal[i]);
            }
        }

        //窗体关闭时退出程序
        private void Signals_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Form.ActiveForm == this) Application.Exit();
        }

        //返回设置界面
        private void button1_Click(object sender, EventArgs e)
        {
            Settings settings = new Settings();
            settings.Show();
            settings.Location = this.Location;
            this.Close();
        }

        //进入自定义信号编辑界面（添加）
        private void button2_Click(object sender, EventArgs e)
        {
            if (Vars.custom_signal_count >= Defines.MAX_CUSTOM_SIGNAL_COUNT)
            {
                MessageBox.Show("自定义信号最多只能有" + Defines.MAX_CUSTOM_SIGNAL_COUNT + "个！");
                return;
            }
            SignalInfo signalInfo = new SignalInfo();
            signalInfo.Show();
            signalInfo.Location = this.Location;
            this.Close();
        }

        //删除自定义信号
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null) { return; }
            string text = listBox1.SelectedItem.ToString();
            for (int i = 0; i < Vars.custom_signal_count; i++)
            {
                if (Vars.custom_signal[i] == text)
                {
                    for (int j = i; j < Vars.custom_signal_count - 1; j++)
                    {
                        Vars.custom_signal[j] = Vars.custom_signal[j + 1];
                    }
                    Vars.custom_signal[Vars.custom_signal_count - 1] = "";
                    Vars.custom_signal_count--;
                    listBox1.Items.Remove(listBox1.SelectedItem);
                    break;
                }
            }
        }

        //双击自定义信号进入自定义信号编辑界面（修改）
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            int n = -1;
            for (int i = 0; i < Vars.custom_signal_count; i++)
            {
                if (Vars.custom_signal[i] == listBox1.SelectedItem.ToString())
                {
                    n = i;
                    break;
                }
            }
            if (n == -1) { return; }
            SignalInfo signalInfo = new SignalInfo(n);
            signalInfo.Show();
            signalInfo.Location = this.Location;
            this.Close();
        }
    }
}

